package com.zrq.myapplication.mvp.Impl;

import com.google.gson.Gson;
import com.zrq.myapplication.business.HttpUntil;
import com.zrq.myapplication.model.Phone;
import com.zrq.myapplication.mvp.MvpMainView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import static com.alibaba.fastjson.JSONObject.parseObject;



public class MainPresenter extends BasePresenter{
    //号码归属地查询接口
    String mUrl = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm";

    MvpMainView mvpMainView;

    Phone mPhone;

    /**
     * presenter构造方法中传入主页面view接口的引用，实现绑定
     */
    public MainPresenter(MvpMainView mainView) {
        mvpMainView=mainView;
    }


    /**
     * 进行号码解析
     */
    public void searchPhoneInfo(String phone){

        if (phone.length()!=11){
            mvpMainView.showToast("请输入正确的手机号");
            return;
        }
        mvpMainView.showLoading();
        //Http请求的处理逻辑
        sendHttp(phone);

    }

    private void sendHttp(String phone){
        Map<String,String> map=new HashMap<>();
        map.put("tel",phone);
        //创建网络请求对象以发送网络请求
        HttpUntil httpUntil=new HttpUntil(new HttpUntil.HttpResponse() {
            /**
             * 成功回调
             */
            @Override
            public void onSuccess(Object object) {
                String json=object.toString();
                int index=json.indexOf("{");
                json=json.substring(index,json.length());


                //基于jsonObject解析
//                mPhone=parseModelWithOrgjson(json);
                //基于Gson解析
//                mPhone=parseModelWithGson(json);
                //基于Fastjson解析
                mPhone=parseModelWithFastJson(json);
                //关闭加载弹窗
                mvpMainView.hideLoading();
                //更新视图
                mvpMainView.updateView();
            }

            /**
             * 失败回调
             */
            @Override
            public void onFail(String error) {
                //弹出错误提示
                mvpMainView.showToast(error);
                //关闭加载动画
                mvpMainView.hideLoading();
            }
        });
        //发送网络请求
        httpUntil.sendGetHttp(mUrl,map);
    }

    /**
     * 基于jsonObject解析
     */
    private Phone parseModelWithOrgjson(String json){
        Phone phone = new Phone();
        try {
            JSONObject jsonObject=new JSONObject(json);

            String value=jsonObject.getString("telString");
            phone.setTelString(value);

            value=jsonObject.getString("province");
            phone.setProvince(value);

           value=jsonObject.getString("catName");
            phone.setCatName(value);

            value=jsonObject.getString("carrier");
            phone.setCarrier(value);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return phone;
    }

    /**
     * 基于Gson解析
     */
    private Phone parseModelWithGson(String json){
        Gson gson=new Gson();
        Phone phone=gson.fromJson(json,Phone.class);
        return phone;
    }

    /**
     * 基于Fastjson解析
     */
    private Phone parseModelWithFastJson(String json){
        Phone phone= parseObject(json,Phone.class);
        return phone;
    }


    /**
     * 获取解析后的号码信息
     */
    public Phone getPhoneInfo(){
        return mPhone;
    }

}
